﻿

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace CCF20Model
{
    public class VehicleMaintenanceRecordData : MaintenanceTypeData
    {

        [DisplayName("上次服务里程")]
        public int? LastServiceMileage { get; set; }
        [DisplayName("上次服务时间"), DataType(DataType.Date)]
        public DateTime? LastServiceDate { get; set; }

        public Int64 VehicleMaintenaceRecordID { get; set; }
        [DisplayName("维护项目描述"), ReadOnly(true)]
        public string Name { get; set; }

        public int VehicleID { get; set; }

    }
    public class MaintenanceTypeData
    {

        [DisplayName("项目编号"), ReadOnly(true)]
        public int MaintenanceTypeID { get; set; }
        [DisplayName("车型")]
        public string VehicleModel { get; set; }
        [DisplayName("生产厂商")]
        public string Manufacture { get; set; }

        [DisplayName("汽车引擎")]
        public string Engine { get; set; }

        [DisplayName("维护项目描述")]
        public string Name { get; set; }

        [DisplayName("项目里程")]
        public int? ServiceMileage { get; set; }

        [DisplayName("项目周期(天)")]
        public int? ServicePeriod { get; set; }
        [DisplayName("车型编号")]
        public int? VehicleTypeID { get; set; }
        [DisplayName("定制")]
        public bool IsCustomized { get; set; }
        [DisplayName("已选")]
        public bool IsChoosen { get; set; }
        [DisplayName("上次服务里程")]
        public int? LastServiceMileage { get; set; }
        [DisplayName("上次服务时间"), DataType(DataType.Date)]
        public DateTime? LastServiceDate { get; set; }
    }


    public class VehicleMaintenanceRecordModel
    {
        CCF20Entities ccf20Ent;

        
        public void SetupVehicleMaintenanceRecord(int? VehicleID,int MaintenanceTypeID,int? ServiceMileage,DateTime? ServiceDate, bool IsCreate)
        {
            VehicleMaintenanceRecord vmr = ccf20Ent.VehicleMaintenanceRecords.Where(v => v.MaintenanceTypeID == MaintenanceTypeID).FirstOrDefault();
            if (vmr != null)
            {
                if (IsCreate)
                {
                    vmr.ServiceMileage = ServiceMileage;
                    vmr.ServiceDate = ServiceDate;
                    ccf20Ent.SaveChanges();
                    return;
                }
                else
                {
                    ccf20Ent.VehicleMaintenanceRecords.DeleteObject(vmr);
                }
            }
            else
            {
                if (IsCreate)
                {
                    vmr = new VehicleMaintenanceRecord();
                    vmr.VehicleID = VehicleID;
                    vmr.MaintenanceTypeID = MaintenanceTypeID;
                    vmr.ServiceMileage = ServiceMileage;
                    vmr.ServiceDate = ServiceDate;
                    
                    ccf20Ent.VehicleMaintenanceRecords.AddObject(vmr);

                }
                else
                {
                    return;
                }
            }
            ccf20Ent.SaveChanges();
        }
        public void UpdateVehicleMaintenanceRecord(int MaintenanceTypeID, int? ServiceMileage, DateTime? ServiceDate)
        {
            VehicleMaintenanceRecord vmr = ccf20Ent.VehicleMaintenanceRecords.Where(v => v.MaintenanceTypeID == MaintenanceTypeID).FirstOrDefault();
            if (vmr != null)
            {
                vmr.ServiceDate = ServiceDate;
                vmr.ServiceMileage = ServiceMileage;
                ccf20Ent.SaveChanges();
               
            }
            else
            {
                throw new Exception(string.Format("UpdateVehicleMaintenanceRecord,Can't find MaintenanceTypeID={0}",MaintenanceTypeID));
            }
        }
        
        public IEnumerable<MaintenanceTypeData> GetVehicleMaintenanceRecord(int? VehicleID)
        {

            Vehicle  vehicle =  ccf20Ent.Vehicles.Where(v=>v.VehicleID == VehicleID).FirstOrDefault();
            if (vehicle == null) return null;
            string Model = vehicle.VehicleType.Model;
            //string Manufacture = vehicle.VehicleType.Manufacture;

            var recordFromCustomized = from c in ccf20Ent.CustomizedMaintenanceTypes
                                       where c.Model.Contains(Model)
                                       join k in (from v in ccf20Ent.VehicleMaintenanceRecords where v.VehicleID == VehicleID select v) 
                                       on c.MaintenanceTypeID equals k.MaintenanceTypeID into leftJoinCustomizedMT
                                       from k in leftJoinCustomizedMT.DefaultIfEmpty()
                                       select new MaintenanceTypeData
                                       {
                                           MaintenanceTypeID = c.MaintenanceTypeID,
                                           VehicleModel = c.Model,
                                           Manufacture = c.Manufacture,
                                           Name = c.Name,
                                           ServiceMileage = c.ServiceMileage,
                                           ServicePeriod = c.ServicePeriod,
                                           Engine = c.EngineType,
                                           IsCustomized = true,
                                           IsChoosen = k != null?true:false,
                                           LastServiceDate = k!=null? k.ServiceDate:null,
                                           LastServiceMileage = k!=null? k.ServiceMileage:null
                                       };

            var recordFromNonCustomized = from c in (ccf20Ent.MaintenanceTypes.Except(from c2 in ccf20Ent.MaintenanceTypes
                                                                                join m in ccf20Ent.CustomizedMaintenanceTypes
                                                                                on c2.MaintenanceTypeID equals m.MaintenanceTypeID
                                                                                select c2))
                                          where c.Model.Contains(Model)
                                          join k in (from v in ccf20Ent.VehicleMaintenanceRecords where v.VehicleID == VehicleID select v)
                                           on c.MaintenanceTypeID equals k.MaintenanceTypeID into leftJoinMT
                                           from k in leftJoinMT.DefaultIfEmpty()
                                           select new MaintenanceTypeData
                                           {
                                               MaintenanceTypeID = c.MaintenanceTypeID,
                                               VehicleModel = c.Model,
                                               Manufacture = c.Manufacture,
                                               Name = c.Name,
                                               ServiceMileage = c.ServiceMileage,
                                               ServicePeriod = c.ServicePeriod,
                                               Engine = c.EngineType,
                                               IsCustomized = false,
                                               IsChoosen = k != null ? true : false,
                                               LastServiceDate = k != null ? k.ServiceDate : null,
                                               LastServiceMileage = k != null ? k.ServiceMileage : null
                                           };

            return from r in recordFromCustomized.Union(recordFromNonCustomized)
                   orderby r.Name
                   select r;

            
        }
        public VehicleMaintenanceRecordModel()
        {
            ccf20Ent = new CCF20Entities();
        }
    }
}
